import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/tips.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_list/site_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/debounce_utils.dart';
import 'mine_site_manager_controller.dart';

class MineSiteManagerPage extends GetView<MineSiteManagerController> {
  const MineSiteManagerPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colour.backgroundColor,
      appBar: AppBarWidget.withRightActions(
        ResKey.siteAdministration.tr,
        actions: [
          IconButton(
            icon: const Icon(Icons.add),
            iconSize: 26,
            onPressed: controller.addNewSite,
          ),
        ],
      ),
      body: GetBuilder<MineSiteManagerController>(
        builder: (_) => SafeArea(
          child: Column(
            children: [

              Container(
                height: 54,
                color: Colors.white,
                child: searchWidget(context),
              ),
              Gaps.vGap10,
              Expanded(
                child: SmartRefresher(
                    enablePullDown: true,
                    enablePullUp: true,
                    controller: controller.refreshController,
                    onRefresh: controller.onRefresh,
                    onLoading: controller.onLoading,
                    header: ListTips.classicHeader,
                    footer: ListTips.generateClassicFooter,
                    child: controller.siteModelList.isEmpty && controller.isLoading?ListEmptyWidget(topPosition: 82):ListView.builder(
                      padding: EdgeInsets.zero,
                      itemBuilder: (BuildContext context, int index) {
                        return _buildItem(controller.siteModelList![index]);
                      },
                      itemCount: controller.siteModelList?.length,
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }

  _buildItem(SiteModel model) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: (){
        debugPrint('基础信息被点击！==${model.siteId}');
        Get.toNamed(RouterPath.siteSiteSettingBaseInfo, arguments: {'siteId': model.siteId??''})?.then((value){
          Log.d('value==$value');
          if(value == true){
            controller.onRefresh();
          }
        });
      },
      child: Container(
        height: 113,
        padding: EdgeInsets.only(left: 16.w, right: 16.w),
        color: Colors.white,
        child: Column(
          children: [
            Row(
              children: [
                Container(
                  height: 80,
                  width: 80,
                  margin: const EdgeInsets.only(top: 16,bottom: 16),
                  decoration: const BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10))),
                  child:  ClipRRect(
                      borderRadius: BorderRadius.circular(6),
                      child:LoadImage(
                        model.logo??'',
                        height: 80,
                        width: 80,
                        fit: BoxFit.cover,
                      )),
                ),
                Gaps.hGap12,
                Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 15,),
                          child: Text(
                            model.name??'',
                            overflow:TextOverflow.ellipsis,
                            maxLines: 1,
                            style: ytTextStyle(
                                fontWeight: FontWeight.w500, fontSize: 16),
                          ),
                        ),
                        Gaps.vGap8,
                        Row(
                          children: [
                            Container(
                              width: 80,
                              margin: const EdgeInsets.only(right: 50),
                              child: Text(
                                ResKey.deliveryTime.tr,
                                style: ytTextStyle(
                                    fontSize: 14, color: Colour.otherTextColor),
                              ),
                            ),
                            Text(
                              model.deliveryTime == null?'':(model.deliveryTime.toString().length>= 10)?
                              model.deliveryTime.toString().substring(0,10):
                              model.deliveryTime.toString(),
                              style: ytTextStyle(fontSize: 14,color: Colour.mainTextColor),
                            ),
                          ],
                        ),
                        Gaps.vGap8,
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Container(
                              padding: const EdgeInsets.only(right: 2),
                              height: 30,
                              // color: Colors.red,
                              alignment: Alignment.topCenter,
                              child: Image.asset(
                                'assets/images/site_base_info_location.png',
                                width: 16.w,
                                height: 16.w,
                              ),
                            ),
                            Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(top: 1),
                                  child: Text(model.address??'',
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                      style: ytTextStyle(
                                          fontSize: 12, color: Colour.otherTextColor)),
                                ))
                          ],
                        ),
                        Gaps.vGap8,
                      ],
                    )),
              ],
            ),
            // const Spacer(),
            Container(
              color: Colour.dividerColor,
              height: 1,
            )
          ],
        ),
      ),
    );
  }

  Widget searchWidget(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: BrnSearchText(
            maxHeight: 40,
            hintStyle: TextStyles.styleWith(16.sp, color: Color(0xFFCCCCCC)),
            textStyle: TextStyles.f16,
            hintText: ResKey.enterSiteName.tr,
            innerColor: Colour.backgroundColor,
            outSideColor: Colors.transparent,
            borderRadius: BorderRadius.circular(6),
            innerPadding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 0),
            onTextChange: DebounceUtils.debounceInput((p0) {
              // controller.searchName = p0;
              // controller.getSiteList();
            }),
            onTextClear: () {
              controller.searchName = '';
              controller.getSiteList();
              return false;
            },
            onTextCommit: (p0) {
              controller.searchName = p0;
              controller.getSiteList();
            },
          ),
        ),

      ],
    );
  }
}
